﻿#region Header

// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System.Linq;
using System.Windows.Controls;
using Majesty_of_Omega.GUI.Pages.Game.Research;
#if NUNIT
using Majesty_of_Omega.Logic.Generator;
using Majesty_of_Omega.Model.Game;
using NUnit.Framework;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute;
#elif MBUNIT
using MbUnit.Framework;
using TestClass = MbUnit.Framework.TestFixtureAttribute;
using TestMethod = MbUnit.Framework.TestAttribute;
using TestInitialize = MbUnit.Framework.SetUpAttribute;
using TestCleanup = MbUnit.Framework.TearDownAttribute;
using ClassInitialize = MbUnit.Framework.TestFixtureSetUpAttribute;
using ClassCleanup = MbUnit.Framework.TestFixtureTearDownAttribute;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif

using Majesty_of_Omega.GUI.Controller;
using Majesty_of_Omega.Config;
using System;
using Majesty_of_Omega.Logic.GameConfiguration;
using Majesty_of_Omega.Model.Report;

#endregion

namespace Test.Majesty_of_Omega.GUI.Pages.Game.Research
{
    /// <summary>
    /// Summary description for TestResearchPageController
    /// </summary>
    [TestFixture]
    public class TestResearchPage : TestWpfBase
    {
        /// <summary>
        /// Ifs the research page will be invoked from the SitReport Entry,
        /// check selected school and tech field.
        /// </summary>
        /// <remarks>
        /// Test for the Techfield "Const" (Construction) in Techschool "PS" (Physics)
        /// </remarks>
        [Test]
        public void CheckSchoolAndTechfieldIfInvokedFromSitReport()
        {
            const string expectedTechSchoolID = "PS";
            const string expectedTechFieldID = "Const";

            AssertCorrectSitRepSetupFor(expectedTechFieldID, expectedTechSchoolID);
        }

        /// <summary>
        /// Ifs the research page will be invoked from the SitReport Entry,
        /// check selected school and tech field.
        /// </summary>
        /// <remarks>
        /// Test for the Techfield "SubPh" (Subatomic Physics) in Techschool "EN" (Energy)
        /// </remarks>
        [Test]
        public void CheckSchoolAndTechfieldIfInvokedFromSitReport2()
        {
            const string expectedTechSchoolID = "EN";
            const string expectedTechFieldID = "SubPh";

            AssertCorrectSitRepSetupFor(expectedTechFieldID, expectedTechSchoolID);
        }

        /// <summary>
        /// Creates a <see cref="ResearchPage"/> and <see cref="ResearchPageController"/>
        /// from a simulated <see cref="SitReport"/>
        /// </summary>
        /// <param name="expectedTechFieldID">The expected tech field ID.</param>
        /// <param name="expectedTechSchoolID">The expected tech school ID.</param>
        private static void AssertCorrectSitRepSetupFor(string expectedTechFieldID, string expectedTechSchoolID)
        {
            TechField testTechField = SpreadSheetManager.TechTables.TechFieldDictionary[expectedTechFieldID];
            SitReportEntry entry = new SitReportEntry(
                SitReportType.ResearchBreakthroughOnField, 
                testTechField, 
                "race1", "Testrace",
                "subject", testTechField.Name,
                "level", 42);
            SaveGame saveGame = TestDataGenerator.TestSaveGame;

            // oh, I miss my tupels
            ResearchPage researchPage;
            ResearchPageController pageController = CreateResearchPageWithController(saveGame, entry, out researchPage);

            Assert.AreEqual(expectedTechFieldID, pageController.SelectedTechField.ResearchItem.ResearchItemTypeID );
            Assert.AreEqual(expectedTechSchoolID, pageController.SelectedTechSchool.ResearchItem.ResearchItemTypeID);

            Assert.AreEqual(expectedTechFieldID, 
                GetSelectedValueFromControl(researchPage, ResearchPage.InternalValue.TechFieldListViewSelectedValue)
                );

        }

        private static ResearchPageController CreateResearchPageWithController(SaveGame saveGame, SitReportEntry entry, out ResearchPage researchPage)
        {
            ResearchPageController pageController = new ResearchPageController(saveGame, entry );
            researchPage = new ResearchPage(pageController);
            return pageController;
        }

        private static string GetSelectedValueFromControl(ResearchPage researchPage, ResearchPage.InternalValue controlId)
        {
            return ((ResearchPageController.TechInfoItem) researchPage.GetInternalValueForUnitTesting(controlId)).ResearchItem.ResearchItemTypeID;
        }
    }
}